<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Sort;
use yii\data\ActiveDataProvider;
use backend\models\odm\TitleOdm as Title;
use yii\filters\AccessControl;

class TitleController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
//                    [
//                        'actions' => ['login', 'captcha'],
//                        'allow' => true,
//                        'roles' => ['?'],
//                    ],
                ],
            ],
        ];
    }
    public function actionIndex() {
        $searchModel = new Title();
        $searchModel->load(Yii::$app->request->queryParams);
        $name = \Yii::$app->request->get('name');
        $sort = \Yii::$app->request->get('sort');
        if(trim($name)){
            $query = Title::find()->where('name like :n')->addParams([':n'=>'%'.$name.'%']);
        }else{
            $query = Title::find();
        }
//        if($sort == 1){
            $query = $query->orderBy('create_time desc');
//        }
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'defaultPageSize' => 10,
            ]
        ]);
        $dataProvider->setSort(false);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'search' => array(
                'name' => $name,
                'sort' => $sort ? $sort : 0,
            ),
        ]);
    }

    /**
     * 新建标题
     * @return type
     */
    public function actionCreate() {
        $this->layout = false;
        $model = new Title();
        if ($model->load(Yii::$app->request->post())) {
            $request = Yii::$app->request->post('TitleOdm');
            $model->create_time = date('Y-m-d H:i:s');
            if ($model->validate()) {
                if ($model->save(false)) {
                    Yii::$app->getSession()->setFlash('success', '保存成功');
                } else {
                    Yii::$app->getSession()->setFlash('error', '保存失败');
                }
                return $this->redirect(['index']);
            } else {
                print_r($model->getErrors());
                die;
            }
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * @param integer $id
     * @return mixed
     */
    public function actionDel($id) {
        $model = $this->findModel($id);
        if ($model->delete()) {
            return $this->redirect(['index']);
        }
    }
    /**
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $this->layout = false;
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post())) {
            $request = Yii::$app->request->post('TitleOdm');
            if ($model->save(false)) {
                Yii::$app->getSession()->setFlash('success', '修改成功');
            } else {
                Yii::$app->getSession()->setFlash('error', '修改失败');
            }
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    protected function findModel($id) {
        if (($model = Title::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    
    /**
     * 删除标题
     */
    public function actionDelTitles()
    {
        if(Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $id_arr = explode(',',$post['ids']);
            $res = Title::deleteAll(['id' => $id_arr]);
            if($res){
                echo "删除成功";
            }else{
                echo "删除失败";
            }
        }
    }
    
}
